package com.kcl.dfss;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

import com.kcl.dfss.adapter.PersonalSetAdapter;
import com.kcl.dfss.http.ServerConnection;
import com.kcl.dfss.utils.AppUtils;
import com.kcl.dfss.utils.WiFiManagerUtils;

public class PersonalSetActivity extends Activity implements OnCheckedChangeListener{

	private AudioManager audioManager;
	private SeekBar seekbar;
	private int maxVolume,currentVolume;
	private CheckBox check_new,check_old,check_sport;
	private TextView text_details;
//	private Switch wifi_switch;
//	private SharedPreferences setting = null;
	private String apname,appwd;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_personal_set);
		
		ListView list_down = (ListView) findViewById(R.id.list_down);
		
		ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String,String>>();
		String data[] = {"软件版本更新检测","常见问题","关于"};
		int type[] = {1,1,1};
		for(int i=0;i<data.length;i++){
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("data", data[i]);
			listItem.add(map);
		}
		
		PersonalSetAdapter personalSetAdapter = new PersonalSetAdapter(listItem, this, type);
		list_down.setAdapter(personalSetAdapter);
		AppUtils.setListViewHeight(list_down);
		list_down.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				switch (position) {
				case 0:
					
					break;
				case 1:
					startActivity(new Intent(PersonalSetActivity.this, FAQActivity.class));
					break;
				case 2:
					startActivity(new Intent(PersonalSetActivity.this, AboutAppActivity.class));
					break;
				default:
					break;
				}
			}
		});
		
		check_new = (CheckBox) findViewById(R.id.check_new);
		check_old = (CheckBox) findViewById(R.id.check_old);
		check_sport = (CheckBox) findViewById(R.id.check_sport);
		text_details = (TextView) findViewById(R.id.text_details);
		
		check_new.setChecked(true);
		check_new.setEnabled(false);
		check_old.setEnabled(false);
		check_sport.setEnabled(false);
		
		/*final SharedPreferences preferences = getSharedPreferences("shared_login", 0);
		final String userid = preferences.getString("userid", "");
		String userpattern = preferences.getString("userpattern", "");
		
		if(userpattern.equals("101")){
			check_new.setChecked(true);
			check_old.setChecked(false);
			check_sport.setChecked(false);
		}else if(userpattern.equals("102")){
			check_old.setChecked(true);
			check_new.setChecked(false);
			check_sport.setChecked(false);
		}else if(userpattern.equals("103")){
			check_sport.setChecked(true);
			check_new.setChecked(false);
			check_old.setChecked(false);
		}
		
		check_new.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					check_old.setChecked(false);
					check_sport.setChecked(false);
					text_details.setText("您现在是新手模式");
					new Thread(new Runnable() {
						@Override
						public void run() {
							String result = ServerConnection.SetPattern(userid, "101");
							try {
								JSONObject jsonObject = new JSONObject(result);
								final String getresult = jsonObject.getString("success");
								runOnUiThread(new Runnable() {
									@Override
									public void run() {
										if(getresult.equals("true")){
											preferences.edit().putString("userpattern", "101").commit();
											Toast.makeText(PersonalSetActivity.this, "设置用户模式成功", Toast.LENGTH_SHORT).show();
										}else{
											Toast.makeText(PersonalSetActivity.this, "设置用户模式失败", Toast.LENGTH_SHORT).show();
										}										
									}
								});
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					}).start();
				}
			}
		});
		check_old.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					check_new.setChecked(false);
					check_sport.setChecked(false);
					text_details.setText("您现在是老手模式");
					new Thread(new Runnable() {
						@Override
						public void run() {
							String result = ServerConnection.SetPattern(userid, "102");
							try {
								JSONObject jsonObject = new JSONObject(result);
								final String getresult = jsonObject.getString("success");
								runOnUiThread(new Runnable() {
									@Override
									public void run() {
										if(getresult.equals("true")){
											preferences.edit().putString("userpattern", "102").commit();
											Toast.makeText(PersonalSetActivity.this, "设置用户模式成功", Toast.LENGTH_SHORT).show();
										}else{
											Toast.makeText(PersonalSetActivity.this, "设置用户模式失败", Toast.LENGTH_SHORT).show();
										}										
									}
								});
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					}).start();
				}
			}
		});
		check_sport.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if(isChecked){
					check_old.setChecked(false);
					check_new.setChecked(false);
					text_details.setText("您现在是运动模式");
					new Thread(new Runnable() {
						@Override
						public void run() {
							String result = ServerConnection.SetPattern(userid, "103");
							try {
								JSONObject jsonObject = new JSONObject(result);
								final String getresult = jsonObject.getString("success");
								runOnUiThread(new Runnable() {
									@Override
									public void run() {
										if(getresult.equals("true")){
											preferences.edit().putString("userpattern", "103").commit();
											Toast.makeText(PersonalSetActivity.this, "设置用户模式成功", Toast.LENGTH_SHORT).show();
										}else{
											Toast.makeText(PersonalSetActivity.this, "设置用户模式失败", Toast.LENGTH_SHORT).show();
										}										
									}
								});
							} catch (JSONException e) {
								e.printStackTrace();
							}
						}
					}).start();
				}
			}
		});*/
		
		//音量调节
		seekbar = (SeekBar) findViewById(R.id.seekBar);
		audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
		
		maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
		seekbar.setMax(8);
		if(currentVolume*100/maxVolume < 47){
			seekbar.setProgress(0);	
			audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 7, 0);
		}else{
			seekbar.setProgress(currentVolume-7);
		}
		
		seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
				
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress+7, 0);
				currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
				seekBar.setProgress(currentVolume-7);
			}
		});
		
		//返回
		LinearLayout personal_set_back = (LinearLayout) findViewById(R.id.personal_set_back);
		personal_set_back.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		
		/*//wifi热点设置
		setting = getSharedPreferences("first_install", 0);
		apname = setting.getString("WIFI_NAME", "MyCamera");
		appwd = setting.getString("WIFI_PWD", "87654321");
		wifi_switch = (Switch) findViewById(R.id.wifi_switch);
		wifi_switch.setOnCheckedChangeListener(this);
		if(WiFiManagerUtils.isWifiApEnabled(PersonalSetActivity.this)){
			wifi_switch.setChecked(true);
		}else{
			wifi_switch.setChecked(false);
		}*/
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		if(isChecked){
			WiFiManagerUtils.setWifiApEnabled(PersonalSetActivity.this, true, apname, appwd);
		}else{
			WiFiManagerUtils.setWifiApEnabled(PersonalSetActivity.this, false, apname, appwd);
		}
	}

}
